********************************
***Fix Message Vary Source***
********************************

clear all

********************
*Set Person Working*
*Change this to your name!!!!!
********************
*global carolina 0
*global emily 1

global emily 1
global carolina 0

*********************
*Load Files*
*********************

*Carolina's Computer*
if $carolina ==0  {
cap cd "E:\Dropbox\Carolina-Emily-Project\Data\Study"
global out "..\..\Results\"
}


if $emily ==1  {
*Emily's Computer*
cap cd "~/Dropbox/SourceContent/Data/Study"
global out "../../Results/"
} 

use 3_clean_data, replace


/*Sample KSmirnov Code*
gen groupk=1 if treatment==1
replace groupk=0 if  treatment==0
ksmirnov ln_prob_level_anti  if democrat==0 & (treatment==1 | treatment==0), by(group)

r(p_1) is probability that the first group distribution is less than the second group
r(p_2) is probability that the first group distribution is greater than the second group
r(p) gives combined probability of difference
*/

*********************************
***Create Variables and Format***
*********************************

global lasso0 hispanic age55_64 candidate16_hillary candidate16_other ///
	immlevel_decrease gun_lessstrict abortion_illegal tax_toohigh health_notgvt ///
	occ_twitter occ_buzzfeed trumpfan lebronfan taylorfan bgatesfan ///
	obamafan obama_neutral topissue_health 
	
global lasso1 black hispanic age35_44 age45_54 hsdegree ///
	candidate16_hillary immlevel_decrease immlevel_same ///
	gun_same gun_lessstrict abortion_partlegal abortion_illegal tax_toohigh ///
	health_neutral health_notgvt i.freq_nytimes ///
	daily_tv occ_tv daily_newspaper i.freq_fox week_breitbart ///
	daily_breitbart occ_buzzfeed ///
	lebron_neutral obamafan trump_neutral trumpfan west topissue_tax 

	
/*Order or Treatment Groups*
5 7 6 8 1 3 2 4 9 10
*/


gen group1=treatment==5 | treatment==7 | treatment==0
gen group2=treatment==6 | treatment==8 | treatment==0
gen group3=treatment==1 | treatment==3 | treatment==0
gen group4=treatment==2 | treatment==4 | treatment==0

global message1 "Anti"
global message2 "Anti"
global message3 "Pro"
global message4 "Pro"

global source1 "Trump"
global source2 "Obama"
global source3 "Trump"
global source4 "Obama" 

foreach y in 1 3 {
	global space`y' "[0.1em]" 
	} 
foreach y in 2 {
	global space`y' "[1em]" 
	} 
foreach y in 4 {
	global space`y' "[0.65em]" 
	} 
	
gen president=treatment==1 | treatment==2 | treatment==5 | treatment==6
gen actor=treatment==3 | treatment==4 | treatment==7 | treatment==8
gen turkey= treatment==9 | treatment==10
gen priming=turkey==1 | president==1
gen message=actor==1 | president==1

global color1 "blue"
global color0 "red"

*gen fan_president=obamafan if treatment==6 | treatment==8 | treatment==2 | treatment==4
*replace fan_president=trumpfan if treatment==5 | treatment==7 | treatment==1 | treatment==3


*****************************
*****************************
*****************************
global party0 "Republicans"
global party1 "Democrats"
global var0 "ln(Probability Anti-Immigrant + 1)"
global var1 "ln(Probability Pro-Immigrant + 1)"
global rule0 "\toprule"
global rule1 "\midrule"
global dir0 "anti"
global dir1 "pro"
global Dir0 "Anti"
global Dir1 "Pro"


****************************************
*TABLES - Pro/Anti VERSION*
****************************************

	
file close _all
capture macro drop fh
file open fh using "${out}4_MessageFixed_VarySource.tex", write replace
file write fh ///
"\hspace*{-1cm}\small{\begin{tabular}{llccccccccc}"  _n 

foreach p in 0 1 {

sum prob_index_${dir`p'} if treat==0 & recruit==`p'
local y`p'=round(`r(mean)'*1000)/1000
local y`p': di %6.3f `y`p''

sum ln1_prob_index_${dir`p'} if treat==0 & recruit==`p'
local ly`p'=round(`r(mean)'*1000)/1000
local ly`p': di %6.3f `ly`p''
	
file write fh ///
"${rule`p'}" _n ///
" & & \multicolumn{9}{c}{\textbf{${party`p'}}}   \\ [0.75em] " _n ///
" & & \multicolumn{9}{c}{\textit{${var`p'}}}   \\ [0.2em] " _n 

file write fh ///
" & & \multicolumn{4}{c}{Anonymous Message} & \multicolumn{4}{c}{Source Persuasion} & \\" _n ///
" \textbf{Message} & \textbf{Source} & $\beta_m$ &  (S.E.) & \%Diff. & \textit{P(Dist.)} & $\beta_{ms}$ &  (S.E.) & \%Diff. & \textit{P(Dist.)} &  N \\" _n ///
"\midrule \\" _n	


foreach y in 1 2 3 4 {
 
	**************************
	*Decomposition Regression*
	**************************
	
	reg ln1_prob_index_${dir`p'} message president ///
		${lasso`p'} if (group`y'==1 ) & recruit==`p', robust

	count if e(sample)
	local n`p'=r(N)

	*Beta M - message*	
	local bm`p' = round(_b[message]*1000)/1000
	local bm`p' : di %6.3f `bm`p''
	local sm`p' = round(_se[message]*1000)/1000
	local sm`p' : di  %6.3f `sm`p''
	local p`p' = 2*ttail(e(df_r),abs(_b[message]/_se[message]))
	if `p`p''<=0.1 & `p`p''>0.05 {
		global starm`p' "*"
		}
	if `p`p''<=0.05 & `p`p''>0.01 {
		global starm`p' "**"
		}
	if `p`p''<=0.01 {
		global starm`p' "***"
		}
	else if `p`p''>0.1 {
		global starm`p' ""
		}
	sum prob_index_${dir`p'} if treatment==0 & e(sample)==1
	local m=r(mean)
	local cm`p'=round(_b[message]*(`m'+1)/`m'*10000)/100
	local cm`p' : di  %6.2f `cm`p''

	
	*Beta MS - President*	
	local bms`p' = round(_b[president]*1000)/1000
	local bms`p' : di %6.3f `bms`p''
	local sms`p' = round(_se[president]*1000)/1000
	local sms`p' : di  %6.3f `sms`p''
	local p`p' = 2*ttail(e(df_r),abs(_b[president]/_se[president]))
	if `p`p''<=0.1 & `p`p''>0.05 {
		global starms`p' "*"
		}
	if `p`p''<=0.05 & `p`p''>0.01 {
		global starms`p' "**"
		}
	if `p`p''<=0.01 {
		global starms`p' "***"
		}
	else if `p`p''>0.1 {
		global starms`p' ""
		}
	sum prob_index_${dir`p'} if president==0 & actor==1 & e(sample)==1
	local m=r(mean)
	local cms`p'=round(_b[president]*(`m'+1)/`m'*10000)/100
	local cms`p' : di  %6.2f `cms`p''
	
	*Distribution Difference - Actor vs. Control*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} ///
		if (group`y'==1 & president==0)  & recruit==`p'
	predict res, res
	gen g=0 if message==0 & e(sample)==1
	replace g=1 if message==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dm`p'=round(r(p)*1000)/1000
	local dm`p': di %6.3f `dm`p''
	drop g res

	
	*Distribution Difference - President vs. Actor*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} if (group`y'==1 & treatment!=0) & recruit==`p'
	predict res, res
	gen g=0 if president==0 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dms`p'=round(r(p)*1000)/1000
	local dms`p': di %6.3f `dms`p''
	drop g res
	
	file write fh ///
	"${message`y'} & ${source`y'} & `bm`p''${starm`p'} & (`sm`p'')  & `cm`p''\% & \textit{`dm`p''} & `bms`p''${starms`p'} & (`sms`p'') & `cms`p''\% & \textit{`dms`p''} & `n`p'' 	\\" _n ///
	"${space`y'}" _n	
	
}

file write fh ///
"  \multicolumn{11}{r}{Control Group Mean: \textit{ln(P(${Dir`p'})+1)}=`ly`p'', \textit{P(${Dir`p'})}=`y`p''}   \\ \\ " _n 


}


file write fh ///
"\bottomrule" _n ///
"\end{tabular}}\hspace*{-1cm}" _n
file close fh





